

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Servlet implementation class deleteRoomService
 */
@WebServlet("/deleteRoomService")
public class deleteRoomService extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteRoomService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		File destinationDir;
		File file;
		String realPath = getServletContext().getRealPath("");
		String filePath;
		int roomID = Integer.parseInt(request.getParameter("roomID"));
		filePath = "/images/rooms/" + roomID + "/";
		destinationDir = new File(realPath + filePath);
		FileUtils.deleteDirectory(destinationDir);
		delRoomPhoto(roomID);
		delRoom(roomID);
	}
	protected int delRoom(int roomID)
	{
		int temp = 0;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        String sqlString = "delete from room where roomID = " + roomID;
			temp = session.createSQLQuery(sqlString).executeUpdate();
			session.getTransaction().commit();
			} catch(Exception ex){
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
	    return temp;
	}
	protected int delRoomPhoto(int roomID)
	{
		int temp = 0;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        String sqlString = "delete from roomphotos where roomID = " + roomID;
			temp = session.createSQLQuery(sqlString).executeUpdate();
			session.getTransaction().commit();
			} catch(Exception ex){
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
	    return temp;
	}
}
